package service

import (
	"time"

	"github.com/go-kit/kit/metrics"
)

type InstrumentingMiddleware struct {
	RequestCount   metrics.Counter
	RequestLatency metrics.Histogram
	CountResult    metrics.Histogram
	Next           IServer
}

func (mw InstrumentingMiddleware) Hello(name string) (output string) {
	defer func(begin time.Time) {
		lvs := []string{"method", "Hello"}
		mw.RequestCount.With(lvs...).Add(1)
		mw.RequestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mw.Next.Hello(name)
}

func (mw InstrumentingMiddleware) Bye(name string) (output string) {
	defer func(begin time.Time) {
		lvs := []string{"method", "Bye"}
		mw.RequestCount.With(lvs...).Add(1)
		mw.RequestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
	}(time.Now())
	return mw.Next.Bye(name)
}
